%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% O-Ring Production Networks
%   Author: Demir, Fieler, Xu, Yang
%   Last updated: January 2021
%
% Purpose: Counterfactual Analysis (No Complementarity)
%   - Resolve initial equilibrium
%   - Baseline Counterfactual
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
close all; diary off; clc; clear; 

    % load full model results
    fullmodel = load('../../../output/temp/fullmodel_counterfactual.mat', 'const', 'eqm_cf1', 'ge_cf1');
   
    

%% Resolve Initial Equilibrium
fprintf('\nInitial Equilibrium\n');
  
    % parameters
    define_param
    define_param_est_nocomplem %add estiamted parameters     

    % data moment
    define_data_moment
    
    % constants
    define_omega_dist %firm type and density
    define_const_nocomplem 
    
    % solve initial equilibrium
    eqm = solve_eqm_outer(param, const);    
    define_wage_schedule %add wage schedule
    
    % calculate model moments and objfcn
    model = cal_model_moment(param, const, eqm); 
    bartik = cal_chg_bartik(param, const, eqm);     
    
    % calculate vars for each firm type
    firm = cal_firm_vars(param, const, eqm);
 
    
    

%% 5 Percent Export Demand Shock

    const.D_F = param.shock*const.D_F;
    
    
    
%% Counterfactual #1: Baseline Counterfactual 
fprintf('\nBaseline Counterfactual\n');

    % solve the equilibrium
    eqm_cf1 = solve_eqm_cf1_outer(param, const, eqm);
    
    % calculate vars for each firm type    
    firm_cf1 = cal_firm_vars(param, const, eqm_cf1);
    
    % counterfactual changes
    ge_cf1 = cal_chg_ge(eqm, firm, eqm_cf1, firm_cf1);       
    
   
   
